package 剑指Offer.easy;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;

public class 从头到尾打印链表 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution {
        public int[] reversePrint(ListNode head) {
            if (head == null) {
                return new int[0];
            }
            Deque<Integer> stack = new ArrayDeque<>();
            while (head != null) {
                stack.push(head.val);
                head = head.next;
            }
            int[] array = new int[stack.size()];
            int index = 0;
            while (!stack.isEmpty()) {
                array[index++] = stack.pop();
            }
            return array;
        }
    }
}
